<?php 

if (strpos($_SERVER['REQUEST_URI'], __FILE__) !== FALSE)
	exit;

class MSAjaxProxyGenerator
{
    function generateClientProxy($obj)
    {
        header("Content-type: application/x-javascript");

        $applicationRoot = dirname($_SERVER['SCRIPT_NAME']);

        $reflect = new ReflectionObject($obj);

        $classname = $reflect->getName();

        echo "var ", $classname, "=function() {\r\n";
        echo $classname, ".initializeBase(this);\r\n";
        echo "this._timeout = 0;\r\nthis._userContext = null;\r\nthis._succeeded = null;\r\nthis._failed = null;\r\n}\r\n";
        echo $classname, ".prototype={\r\n";

        $second = 0;
        foreach ($reflect->getMethods() as $m => $method)
        {
            if (!$method->isStatic() && $method->isPublic() &&
                $method->getDeclaringClass()->getName() != 'MSAjaxService')
            {
                if ($second == 1) { echo ",\r\n"; }
                echo $method->getName(), ":function(";
                foreach ($method->getParameters() as $p => $param)
                {
                    echo $param->getName(), ",";
                }
                echo "succeededCallback, failedCallback, userContext) {\r\n";
                echo "return this._invoke(", $classname, ".get_path(), '", $method->getName(), "',false,{";
                $second = 0;
                foreach ($method->getParameters() as $p => $param)
                {
                    if ($second == 1) { echo ","; }
                    echo $param->getName(), ":", $param->getName();
                    $second = 1;
                }
                echo "},succeededCallback,failedCallback,userContext); }";
                $second = 1;
            }
        }
        echo "}\r\n";

        echo $classname, ".registerClass('", $classname, "',Sys.Net.WebServiceProxy);\r\n";
        echo $classname, "._staticInstance = new ", $classname, "();\r\n";
        echo $classname, ".set_path = function(value) { ", $classname, "._staticInstance._path = value; }\r\n";
        echo $classname, ".get_path = function() { return ", $classname, "._staticInstance._path; }\r\n";
        echo $classname, ".set_timeout = function(value) { ", $classname, "._staticInstance._timeout = value; }\r\n";
        echo $classname, ".get_timeout = function() { return ", $classname, "._staticInstance._timeout; }\r\n";
        echo $classname, ".set_defaultUserContext = function(value) { ", $classname, "._staticInstance._userContext = value; }\r\n";
        echo $classname, ".get_defaultUserContext = function() { return ", $classname, "._staticInstance._userContext; }\r\n";
        echo $classname, ".set_defaultSucceededCallback = function(value) { ", $classname, "._staticInstance._succeeded = value; }\r\n";
        echo $classname, ".get_defaultSucceededCallback = function() { return ", $classname, "._staticInstance._succeeded; }\r\n";
        echo $classname, ".set_defaultFailedCallback = function(value) { ", $classname, "._staticInstance._failed = value; }\r\n";
        echo $classname, ".get_defaultFailedCallback = function() { return ", $classname, "._staticInstance._failed; }\r\n";

        $pathWithoutProxy = $_SERVER['REQUEST_URI'];
        $pathWithoutProxy = substr($pathWithoutProxy, 0, strlen($pathWithoutProxy) - 3);
        $pathWithoutProxy = addslashes($pathWithoutProxy);
        echo $classname, ".set_path(\"", $pathWithoutProxy,"\");\r\n";

        foreach ($reflect->getMethods() as $m => $method)
        {
            if (!$method->isStatic() && $method->isPublic() &&
                $method->getDeclaringClass()->getName() != 'MSAjaxService')
            {
                echo $classname, ".", $method->getName(), "= function(";
                $params = '';
                foreach ($method->getParameters() as $p => $param)
                {
                    $params .= $param->getName() . ',';
                }
                echo $params, "onSuccess,onFailed,userContext) {", $classname, "._staticInstance.", $method->getName(), "(", $params, "onSuccess,onFailed,userContext); }\r\n";
            }
        }
    }
}
?>